﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TTQT;
using TTQT.TTQT_Code.Info;

public partial class HorizontalMenu_New : MasterControl// System.Web.UI.UserControl
{
    StringBuilder b = new StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["strMenu"] != null)
        {
            lblMenu.Text = Session["strMenu"].ToString();
            return;
        }
        b.Append("<div id=\"nav\" style=\"text-align:left;\"><ul class=\"clearfix\">");
        try
        {
            if (Session["UserId"] != null)
            {
                //this.UserInfo this.UserInfo = TTQT.TTQT_Code.DataAccessObject.instance().getUser(TTQT.TTQT_Code.TTQTCommon.ToInt32(Session["UserId"].ToString(), -1));

                ArrayList listCmd;
                if (this.UserInfo.isSuperAdmin.Equals("1"))
                {
                    listCmd = TTQT.TTQT_Code.DataAccessObject.instance().getAllMenu();
                }
                else
                {
                    listCmd = TTQT.TTQT_Code.DataAccessObject.instance().GetArrayCmdsByUser_Id(this.UserInfo.userId);
                }
                if (listCmd.Count < 1)
                {
                    b.Append("<center>&nbsp;<span style='color:yellow;font-weight:bold;'>NGƯỜI DÙNG NÀY CHƯA ĐƯỢC PHÂN QUYỀN. VUI LÒNG LIÊN HỆ VỚI NGƯỜI QUẢN TRỊ TRỰC TIẾP!</span></center>");
                    return;
                }
                bool truocDoLaCha = false, truocDoLaCon = false;
                for (int i = 0; i < listCmd.Count; i++)
                {
                    CmdInfo cmd = (CmdInfo)listCmd[i];
                    //Hien tai la cha
                    if (cmd.Cmd_Parent_Id == 0)
                    {
                        if (!truocDoLaCha & !truocDoLaCon)
                        {
                            b.Append("<li>");
                            if (cmd.Cmd_Path.Equals(""))
                            {
                                addMenuCha(cmd.Cmd_Name, cmd.Cmd_Id, true);
                            }
                            else
                            {
                                addMenuCha(cmd.Cmd_Name, cmd.Cmd_Id, true);
                            }
                        }
                        if (!truocDoLaCha && truocDoLaCon)
                        {
                            b.Append("</ul></li><li>");
                            if (cmd.Cmd_Path.Equals(""))
                            {
                                addMenuCha(cmd.Cmd_Name, cmd.Cmd_Id, true);
                            }
                            else
                            {
                                addMenuCha(cmd.Cmd_Name, cmd.Cmd_Id, true);
                            }
                        }
                        if (truocDoLaCha)
                        {
                            b.Append("</li><li>");
                            if (cmd.Cmd_Path.Equals(""))
                            {
                                addMenuCha(cmd.Cmd_Name, cmd.Cmd_Id, true);
                            }
                            else
                            {
                                addMenuCha(cmd.Cmd_Name, cmd.Cmd_Id, true);
                            }
                        }
                        truocDoLaCha = true;
                        truocDoLaCon = false;
                    }
                    else // Hiện tại là con
                    {
                        if (truocDoLaCha)
                        {
                            b.Append("<ul>");
                            addMenuCon(cmd.Cmd_Name, cmd.Cmd_Id);
                        }
                        if (truocDoLaCon)
                        {
                            addMenuCon(cmd.Cmd_Name, cmd.Cmd_Id);
                        }
                        truocDoLaCon = true;
                        truocDoLaCha = false;
                    }
                }
                if (truocDoLaCha)
                {
                    b.Append("</li>");
                }
                if (truocDoLaCon)
                {
                    b.Append("</ul></li>");
                }
            }
        }
        catch (Exception ex)
        {
            //string err=ex.ToString();
            //b.Append(err);
        }
        b.Append("</ul></div>");
        Session["strMenu"] = b.ToString();
        lblMenu.Text = b.ToString();

    }
    public void addMenuCon(string menuName, int menuId)
    {
        if (menuId == 93)
        {
            if (UserInfo.Brn_Code.Equals("990"))
            {
                b.Append("<li><a href=\"Default.aspx?pageId=" + menuId + "\">" + menuName + "</a></li>");
            }
        }
        else
        {
            b.Append("<li><a href=\"Default.aspx?pageId=" + menuId + "\">" + menuName + "</a></li>");
        }
    }
    public void addMenuCha(string menuName, int menuId, bool disable)
    {
        if (disable)
        {
            b.Append("<a href=\"#\">" + menuName + "</a>");
        }
        else
        {
            b.Append("<a href=\"Default.aspx?pageId=" + menuId + "\">" + menuName + "</a>");
        }
    }
}
